package com.whales.lms.rms.scheduler.task;

import javax.inject.Inject;

import org.springframework.stereotype.Component;

import com.whales.lms.rms.scheduler.TaskTemplate;
import com.whales.lms.rms.web.model.FaultVO;
import com.whales.lms.rms.web.model.ScheduleVO;
import com.whales.lms.rms.web.service.FaultService;
import com.whales.lms.rms.web.service.ScheduleService;
import com.whales.lms.rms.web.util.ConstantUtil;
import com.whales.shared.util.AgentUtil;
import com.whales.shared.util.XmlUtil;

@Component
public class AgentAliveCheckTask extends TaskTemplate {
	@Inject
	private AgentUtil agentUtil;
	@Inject
	private ScheduleService scheduleService;
	@Inject
	private FaultService faultService;
	
	public AgentAliveCheckTask() {
		//반복주기로 setting할 경우
//        this.setDelay(60000 * 5); //5분 간격
        //cron 형태로 setting할 경우
        this.setConExpression("0 0/5 * * * ?"); //5분 간격
	}
	
	@Override
	public void execute() throws Exception {
		//PC List를 조회
		for (ScheduleVO toLivePc : scheduleService.getToLivePcListByTime()) {
			if(!agentUtil.sendXml(toLivePc.getIp(), XmlUtil.buildDocForServerCommand("ALIVE")).equals("OKE")){
				FaultVO faultVO = new FaultVO();
				faultVO.setId(toLivePc.getIp());
				faultVO.setSepType("HW");
				faultVO.setFaultCode(ConstantUtil.STATE_HNE);
				faultService.create(faultVO);
			}
		}
	}

}
